package com.runda.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import tk.mybatis.mapper.entity.Example;

import com.bbjob.dto.Result;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.mapper.SysOrganizationStuMapper;
import com.bbjob.model.SysOrganization;
import com.bbjob.model.SysOrganizationStu;
import com.github.pagehelper.PageInfo;
import com.runda.query.QueryExaminee;
import com.runda.service.IExamineeService;

@Api(tags="examineeApi", description="考生管理")
@RestController
@RequestMapping(path="examinee", produces="application/json;charset=utf-8")
public class ExamineeController {
	
	@Resource
	private IExamineeService examineeService;

	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	
	@Resource
	private SysOrganizationStuMapper sysOrganizationStuMapper;
	
	@ApiOperation(value="考生录入", notes="考生录入")
	@ApiImplicitParam(name="stu", value="考生录入", required=true, dataType="SysOrganizationStu")
	@RequestMapping(value="entry", method=RequestMethod.POST)
	public Result entryExaminee(@RequestBody SysOrganizationStu stu) {
		
		if (stu.getUniquecode() != null && !"".equals(stu.getUniquecode())) {
			Example example = new Example(SysOrganization.class);
			example.createCriteria().andEqualTo("uniqueCode", stu.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(example);
			if (organs.size() > 0) {
				stu.setOrganSchool(organs.get(0).getId());
				stu.setSchoolName(organs.get(0).getName());
			}
			
			Example sExample = new Example(SysOrganizationStu.class);
			sExample.createCriteria().andEqualTo("sno", stu.getSno());
			List<SysOrganizationStu> stus = sysOrganizationStuMapper.selectByExample(sExample);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			try {
				if (stu.getBirthdayStr() != null && !"".equals(stu.getBirthdayStr())) {
					stu.setBirthday(sdf.parse(stu.getBirthdayStr()));
				}
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(8106, "日期转换错误");
			}
			
			if (stu.getOrganDepart() != null) {
				SysOrganization sys = sysOrganizationMapper.selectByPrimaryKey(stu.getOrganDepart());
				if (sys != null) {
					stu.setDepartName(sys.getName());
				}
			}
			
			if (stu.getOrganGrade() != null) {
				SysOrganization sys = sysOrganizationMapper.selectByPrimaryKey(stu.getOrganGrade());
				if (sys != null) {
					stu.setGradeName(sys.getName());
				}
			}
			
			if (stu.getOrganClass() != null) {
				SysOrganization sys = sysOrganizationMapper.selectByPrimaryKey(stu.getOrganClass());
				if (sys != null) {
					stu.setClassName(sys.getName());
				}
			}
			

			if (stus.size() > 0) {
				stu.setId(stus.get(0).getId());
				examineeService.updateAll(stu);
			} else {
				examineeService.save(stu);
			}
			
			return new Result(stu);
		} else {
			return new Result(8106, "学校唯一编码为空，无法查询学校信息");
		}
	}


	@ApiOperation(value="考生导入", notes="考生导入")
	@RequestMapping(value="import", method=RequestMethod.POST)
	public Map<String, Object> importExaminee(HttpServletRequest request,  @RequestParam(value="file", required=false) MultipartFile multipartFile) throws Exception {
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		
		try {
			returnMap = examineeService.importExaminee(multipartFile, request);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			
			returnMap.put("code", 8106);
			returnMap.put("message", "导入出现异常，请查看控制台信息");
			returnMap.put("data", null);
		}
		
		return returnMap;
	}
	
	@ApiOperation(value="考生列表", notes="考生列表")
	@ApiImplicitParam(name="examinee", value="考生信息", required=true, dataType="QueryExaminee")
	@RequestMapping(value="list", method=RequestMethod.POST)
	public Result list(@RequestBody QueryExaminee examinee) {
		
		List<SysOrganizationStu> data = examineeService.selectByParams(examinee);
		PageInfo<SysOrganizationStu> pageInfo = new PageInfo<SysOrganizationStu>(data);
		
		return new Result(pageInfo);
	}
	
	@ApiOperation(value="考生列表（httpClient）", notes="考生列表--httpClient用")
	@ApiImplicitParam(name="examinee", value="考生信息", required=true, dataType="QueryExaminee")
	@RequestMapping(value="list_client", method=RequestMethod.POST)
	public List<SysOrganizationStu> list_client(@RequestBody QueryExaminee examinee) {
		
		List<SysOrganizationStu> data = examineeService.selectByParams(examinee);
		
		return data;
	}
	
	@ApiOperation(value="考生信息", notes="考生信息")
	@ApiImplicitParam(name="id", value="考生信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="get/{id}", method=RequestMethod.GET)
	public Result get(@PathVariable("id")Integer id) {
		
		return new Result(examineeService.selectByPrimaryKey(id));
	}
	
	@ApiOperation(value="修改考生信息", notes="修改考生信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="考生信息id", required=true, dataType="Integer", paramType="path"),
			@ApiImplicitParam(name="stu", value="考生信息", required=true, dataType="SysOrganizationStu")	
	})
	@RequestMapping(value="edit/{id}", method=RequestMethod.POST)
	public Result edit(@PathVariable("id")Integer id, @RequestBody SysOrganizationStu stu) {
		
		stu.setId(id);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if (stu.getBirthdayStr() != null && !"".equals(stu.getBirthdayStr())) {
				stu.setBirthday(sdf.parse(stu.getBirthdayStr()));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(8106, "日期转换错误");
		}
		
		if (stu.getOrganDepart() != null) {
			SysOrganization sys = sysOrganizationMapper.selectByPrimaryKey(stu.getOrganDepart());
			if (sys != null) {
				stu.setDepartName(sys.getName());
			}
		}
		
		if (stu.getOrganGrade() != null) {
			SysOrganization sys = sysOrganizationMapper.selectByPrimaryKey(stu.getOrganGrade());
			if (sys != null) {
				stu.setGradeName(sys.getName());
			}
		}
		
		if (stu.getOrganClass() != null) {
			SysOrganization sys = sysOrganizationMapper.selectByPrimaryKey(stu.getOrganClass());
			if (sys != null) {
				stu.setClassName(sys.getName());
			}
		}
		
		this.examineeService.updateNotNull(stu);
		
		return new Result();
	}
	
	@ApiOperation(value="删除考生信息", notes="删除考生信息")
	@ApiImplicitParam(name="id", value="考生信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="del/{id}", method=RequestMethod.GET)
	public Result del(@PathVariable("id")Integer id) {
		
		examineeService.delete(id);
		
		return new Result();
	}
}
